package com.itouchtv.yang.core.tools;

import com.itouchtv.yang.core.Enum.ResultEnum;
import com.itouchtv.yang.core.driver.BaseDriver;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.util.Objects;

public class Screen {

    @Deprecated
    public static void getScreen(BaseDriver dr,String filename){
        WebDriver driver = dr.getWebDriver();
        String cyrPatn=System.getProperty("user.dir");
        File scrfile=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        try {
            com.google.common.io.Files.copy(scrfile, new File(cyrPatn+"\\img\\"+filename+".png"));
//            Fileutils.copyFile(scrfile, new File(cyrPatn+"\\img\\"+filename+".png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("GetScreenshot Fail");
        }finally{
            System.out.println("GetScreenshot Successful"+cyrPatn+"\\img\\"+filename+".png");
        }

    }

    public static String ScreenShot(BaseDriver dr, ResultEnum result) {
        if (Objects.isNull(dr)) return "";
        WebDriver driver = dr.getWebDriver();
        File fileSource = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String filepath = "test-output/";
        String path1 = "";
        String path = "screen" + MyDate.getDate();
        switch (result) {
            case PASS:
                path1 = path + "/pass" + MyDate.getDateTime() + ".jpg";
                break;
            case FIAL:
                path1 = path + "/fail" + MyDate.getDateTime() + ".jpg";
                break;
            case SKIP:
                path1 = path + "/skip" + MyDate.getDateTime() + ".jpg";
                break;
            case CLICK:
                path1 = path + "/click" + MyDate.getDateTime() + ".jpg";
        }
        File file = new File(filepath + path);
        try {
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(filepath + path1);
            if (file.exists()) {
                file.delete();
            }
            Files.copy(fileSource.toPath(), file.toPath());
            Log.logger.info("截图保存成功:"+path1);
            return path1;
        } catch (IOException | InvalidPathException e) {
            Log.logger.error("截图失败！"+"\n"+e.fillInStackTrace());
            e.printStackTrace();
        }
        return "";
    }
}
